package org.example.bookstore.service.impl;

import org.example.bookstore.custom.ResultVO;
import org.example.bookstore.mapper.AddressMapper;
import org.example.bookstore.pojo.DO.Address;
import org.example.bookstore.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    //用户添加地址
    @Override
    public ResultVO<?> insertAddress(Address address) {
        boolean IsOk=addressMapper.insertAddressAll(address);
        if (IsOk){
            return ResultVO.success("地址添加成功");
        }
        return ResultVO.error("地址添加失败");
    }

    //根据id删除地址
    @Override
    public ResultVO<?> delAddress(Integer id) {
        boolean isok=addressMapper.delAddressById(id);
        return isok ? ResultVO.success("删除成功") : ResultVO.error("删除失败");
    }


    //通过id查询用户所有地址
    @Override
    public ResultVO<?> getAddress(Integer id) {
        List<Address> AddressList=new ArrayList<>();
        AddressList=addressMapper.getAddressByUserId(id);
        return ResultVO.success("获取成功地址列表",AddressList);
    }

    //通过地址id对地址进行修改
    @Override
    public ResultVO<?> UpdateAddress(Address address) {
        boolean isok=addressMapper.UpdateAddressById(address);
        return isok ? ResultVO.success("修改成功") : ResultVO.error("修改失败");
    }
}
